<?php 
include('includes/confirm.php');
$page_name = friends;
require_once 'classes/Mysql.php';
//require_once 'includes/constants.php';
$mysql = New Mysql();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include('includes/head.php'); ?>
<body>
<div id="wrapper">
    <?php include('includes/header.php'); ?>
    <?php include('includes/menu.php'); ?>
    
    <div id="page">
    	
	      <div id="content">
			<div id="browse_result">
				<form method="post" action="">
				<table class="sortable" id=anyid cellpadding="0" cellspacing="0">
					<tr>
						<th class="unsortable">User Info</th>
						<th class="unsortable">Details</th>
					</tr>
			<?php
				if($_POST && !empty($_POST['borrow_setting']) && !empty($_POST['view_setting']) && !empty($_POST['user_id'])){
					$stmt=$mysql->run_query("UPDATE friend
																		SET borrow_setting=?, view_setting=?
																		WHERE id1=? AND id2=?", $_POST['borrow_setting'], $_POST['view_setting'], $_SESSION['userid'], $_POST['user_id']);
					echo "<h4>Settings updated!!<h4><br>";
					$stmt->close();
					$updated_friend=true;
				}
				if($_GET && !empty($_GET['uname']) || $updated_friend == true){
					// get settings
					// get user info
					$stmt=$mysql->run_query("SELECT u.fname, u.lname, u.mi, u.email, u.address, u.uname, u.phone, u.dob, u.id 
																		FROM user u
																		WHERE u.uname=?",$_GET['uname']);
					$stmt->bind_result($_fname, $_lname, $_mi, $_email, $_address, $_uname, $_phone, $_dob, $_id);
					if($stmt->fetch()){
						// get the borrow and view settings
						$stmt->close();
						$stmt=$mysql->run_query("SELECT borrow_setting, view_setting 
																			FROM friend 
																			WHERE id1=? 
																				AND id2=?", $_SESSION['userid'], $_id);
						$stmt->bind_result($_borrow_setting, $_view_setting);
						$stmt->fetch();
						$stmt->close();
						$stmt=$mysql->run_query("SELECT id, name
																			FROM share_setting");
						$stmt->bind_result($_id_setting, $_name_setting);
						$setting_id=array();
						$setting_name=array();
						$size=0;
						while($stmt->fetch()){
								$setting_id[] = $_id_setting;
								$setting_name[] = $_name_setting;
								$size++;
						}
						$stmt->close();
						print "<input type='hidden' name='user_id' value=$_id />";
						print "<tr> <td>User Name</td> 			<td>$_uname</td> 								</tr>
									 <tr> <td>Name</td> 					<td>$_lname $_fname $_mi</td> 	</tr>
									 <tr> <td>Email</td> 					<td>$_email</td> 								</tr>
									 <tr> <td>Address</td> 				<td>$_address</td> 							</tr>
									 <tr> <td>Phone</td>					<td>$_phone</td>								</tr>
									 <tr> <td>DOB</td>						<td>$_dob</td>									</tr>
									 <tr> <td>Borrow Setting</td>	<td><select name='borrow_setting'>";
																										for ($i=0; $i<$size; ++$i){
																											if($setting_id[$i] == $_borrow_setting)
																												print "<option selected value= $setting_id[$i]>$setting_name[$i]</option>";
																											else
																												print "<option value=$setting_id[$i]>$setting_name[$i]</option>";
																											
																										}
						print "																	</select></td>								</tr>
									 <tr> <td>View Setting</td>		<td><select name='view_setting'>";
																										for ($i=0; $i<$size; ++$i){
																											if($setting_id[$i] == $_view_setting)
																												print "<option selected value=$setting_id[$i]>$setting_name[$i]</option>";
																											else
																												print "<option value=$setting_id[$i]>$setting_name[$i]</option>";
																										}
						print "																	</select></td>								</tr>";
						
						}
						else{
						$stmt->close();
						echo "<h4>ERROR: Please go back!<h4>";
						}
					
					}
					else{
						echo "<br><br><h4>Invalid Link!!<h4>";
					}
				
			?>
			</table><br>
			<input type="submit" name="update_user_setting" value="Update">
			</form><br>
			</div>
	      </div>
	      <!-- end div#content -->
	      <div id="sidebar">
			<ul>
				<li><h1><a href="friends.php">Browse</a></h1><br></li>
				<li><h1><a href="add_friend.php">Add</a></h1><br></li>
				<li><h1><a href="delete_friend.php">Delete</a></h1><li>
			</ul>
	      </div>
	      <!-- end div#sidebar -->
	      <div style="clear: both; height: 1px"></div>
	   
	  </div>
  
  	</div>
  	<!-- end div#page -->
    <?php include('includes/footer.php'); ?>
</div> <!-- end div#wrapper -->
</body>
</html>
